package com.controller;

import java.io.Serializable;

import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.Util.FacesUtils;
import com.facade.UserFacade;
import com.model.User;

@SuppressWarnings("serial")
@ManagedBean
@SessionScoped
public class Register implements Serializable {

	@EJB
	private UserFacade userFacade;
	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String createUser(User user) {
		if (userFacade.findUserByLogin(user.getLogin()) == null) {
			try {				
				userFacade.saveUser(user);
				this.user = user;
			} catch (EJBException e) {
				return null;
			}
		} else {
			String msg = "This User Already Exists";
			FacesUtils.addErrorMessage(msg);
			return null;
		}
		return "/pages/public/success.xhtml?faces-redirect=true";
	}
}
